package  com.syqc.sensor.water

import androidx.core.view.isVisible
import androidx.drawerlayout.widget.DrawerLayout
import androidx.fragment.app.Fragment
import com.google.android.material.tabs.TabLayout
import com.gyf.immersionbar.ktx.immersionBar
import com.syqc.comlib.base.BaseActivity
import com.syqc.comlib.base.BaseBarActivity
import com.syqc.comlib.utils.MLog
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.utils.Keys
import com.syqc.interfaces.SimpleTabLayoutListener
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityOilInfo2Binding
import com.syqc.monitor.databinding.IncludeTabLayoutBinding
import com.syqc.utils.DataUtil

class WaterInfoActivity :
    BaseActivity<ActivityOilInfo2Binding>(ActivityOilInfo2Binding::inflate) {
    private lateinit var plate: String
    private val mFragments by lazy { ArrayList<Fragment>() }

    private lateinit var mTabVB: IncludeTabLayoutBinding
    private lateinit var carId: String


    override fun initView() {
        immersionBar { titleBar(binding.listToolbar) }
        mTabVB = IncludeTabLayoutBinding.bind(binding.root)
        plate = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        carId = intent.getStringExtra(Keys.INTENT_OTHER) ?: ""
        binding.ivToolbarEnd.isVisible = false
        binding.tvToolbarTitle.text = plate
        binding.listDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
        val dayStats = DataUtil.dayStats
        dayStats?.forEachIndexed { index, statsType ->
            MLog.e("shuju ===================$index")
            mFragments.add(WaterInfoFragment.getInstance(plate, carId, index))
            mTabVB.tabLayout.addTab(mTabVB.tabLayout.newTab())
            mTabVB.tabLayout.getTabAt(index)?.text = statsType.name
        }
        showFragment(mFragments[0])
        if ((dayStats?.size ?: 0) < 2) {
            mTabVB.tabLayout.isVisible = false
        }

    }

    private var currentFragment: Fragment? = null
    private fun showFragment(fragment: Fragment) {
        if (currentFragment != null && currentFragment == fragment) return
        supportFragmentManager.beginTransaction().apply {
            currentFragment?.let { hide(it) }
            currentFragment = fragment
            if (fragment.isAdded) {
                show(fragment)
            } else {
                add(R.id.content, fragment).show(fragment)
            }
        }.commit()
    }


    override fun setListener() {
        binding.ivToolbarStart.setOnClickListener {
            if (isHome) {
                startHomeActivity()
            }
            finish()
        }
        mTabVB.tabLayout.addOnTabSelectedListener(object : SimpleTabLayoutListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                showFragment(mFragments[tab?.position ?: 0])
            }
        })
    }


    override fun onDestroy() {
        super.onDestroy()
        currentFragment = null
        DataUtil.dayStats = null
    }
}